<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addCateBtnFn"
          >添加分类</el-button
        >
      </div>

      <el-table :data="cateList" style="width: 100%" border stripe>
        <el-table-column
          label="序号"
          width="100"
          type="index"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="updateFn(scope.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="removeFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加分类的对话框 -->
      <el-dialog
        :title="isEdit?'修改分类':'增加分类'"
        :visible.sync="addVisible"
        width="50%"
        @closed="addDialogClosed"
      >
        <!-- 添加的表单 -->
        <el-form
          :model="addForm"
          :rules="addRules"
          ref="addRef"
          label-width="80px"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input
              v-model="addForm.cate_name"
              minlength="1"
              maxlength="10"
            ></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input
              v-model="addForm.cate_alias"
              minlength="1"
              maxlength="15"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancleFn">取 消</el-button>
          <el-button type="primary" @click="confirmFn">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { addCateListAPI, delArtCateAPI, getArtCateListAPI, updateArtCateAPI } from '@/api'
export default {
  data () {
    return {
      // 文章分类的列表
      cateList: [],
      // 添加对话框的显示隐藏
      addVisible: false,
      // 添加表单数据
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRef: '',
      addRules: {
        // 添加表单的验证规则对象
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      },
      // 修改的商品的id
      editId: '',
      // 是否修改false增加，true修改
      isEdit: false
    }
  },
  created () {
    this.initCateListFn()
  },
  methods: {
    async initCateListFn () {
      const { data: res } = await getArtCateListAPI()
      if (res.code !== 0) {
        return false
      }
      this.cateList = res.data
      // console.log(this.cateList)
    },
    confirmFn () {
      this.$refs.addRef.validate(async (valid) => {
        if (!valid) {
          return false
        }
        if (this.isEdit) {
          const editForm = {
            id: this.editId,
            ...this.addForm
          }
          const { data: res } = await updateArtCateAPI(editForm)
          if (res.code !== 0) {
            return this.$message.error(res.message)
          }
          this.$message.success(res.message)
          // console.log(res)
        } else {
          const { data: res } = await addCateListAPI(this.addForm)
          console.log(res)
          if (res.code !== 0) {
            return this.$message.error(res.message)
          }
          this.$message.success(res.message)
        }
        this.initCateListFn()
      })
      this.addVisible = false
    },
    cancleFn () {
      this.addVisible = false
    },
    addDialogClosed () {
      this.$refs.addRef.resetFields()
    },
    // 修改
    updateFn (row) {
      console.log(row)
      this.editId = row.id
      this.isEdit = true
      this.addVisible = true
      this.$nextTick(() => {
        this.addForm.cate_name = row.cate_name
        this.addForm.cate_alias = row.cate_alias
      })
    },
    addCateBtnFn () {
      this.editId =
      this.isEdit = false
      this.addVisible = true
    },
    async removeFn (row) {
      const { data: res } = await delArtCateAPI(row.id)
      if (res.code !== 0) {
        return this.$message.error('删除分类失败')
      }
      this.$message.success('删除分类成功')
      this.initCateListFn()
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
